package common.util.redis.service.impl;


import com.google.common.collect.Lists;
import common.util.ByteArrayHelper;
import common.util.redis.service.JedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import redis.clients.util.Pool;

import java.util.*;

public class JedisServiceImpl implements JedisService {
    private Logger logger = LoggerFactory.getLogger(JedisServiceImpl.class);

    protected Pool<Jedis> jedisPool;

    @Override
    public Long del(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.del(key);
        } catch (Exception e) {
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }


    @Override
    public Pool<Jedis> getJedisPool() throws Exception {
        return jedisPool;
    }

    @Override
    public String getStr(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.get(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long hdel(String key, String field) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hdel(key, field);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }


    @Override
    public Long hset(String key, String field, Object value, int seconds) throws Exception {

        Jedis jedis = getJedisPool().getResource();
        try {
            Long ret = jedis.hset(key.getBytes(), field.getBytes(), ByteArrayHelper.object2Bytes(value));
            if (seconds > 0) {
                jedis.expire(key, seconds);
            }
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    public Long hlen(final String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hlen(key);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public boolean isExist(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return false;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Object lindex(String key, int index) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return ByteArrayHelper.byte2Object(jedis.lindex(key.getBytes(), index));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }


    @Override
    public String spop(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return new String(jedis.spop(key.getBytes()));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long sadd(String key, String... member) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.sadd(key, member);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public void lpush(String key, Object value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            jedis.lpush(key.getBytes(), ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<Object> lrange(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Object> ret = new ArrayList<Object>();
        try {
            List<byte[]> list = jedis.lrange(key.getBytes(), start, end);
            for (byte[] i : list) {
                ret.add(ByteArrayHelper.byte2Object(i));
            }
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public void lset(String key, int index, Object value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.lset(key.getBytes(), index, ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }


    public Long rpushStr(String key, String value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.rpush(key, value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public void set(String key, Object value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.set(key.getBytes(), ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }

    }

    @Override
    public void set(String key, Object value, int seconds) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.setex(key.getBytes(), seconds, ByteArrayHelper.object2Bytes(value));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public void setJedisPool(Pool<Jedis> jedisPool) throws Exception {
        this.jedisPool = jedisPool;
    }

    @Override
    public void setStr(String key, String value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public String setexStr(String key, String value, int seconds) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        String res = null;
        try {
            res = jedis.setex(key, seconds, value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            throw e;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return res;
    }

    public Long setNxStr(String key, String value, int seconds) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        Long res = 0l;
        try {
            res = jedis.setnx(key, value);
            if (res == 1l) {
                jedis.expire(key, seconds);
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            this.del(key);
            throw e;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return res;
    }

    @Override
    public Long setNxStr(String key, String value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        Long res = 0l;
        try {
            res = jedis.setnx(key, value);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            throw e;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return res;
    }

    @Override
    public Long zadd(String key, double score, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zadd(key.getBytes(), score, ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zadd2(String key, double score, long member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zadd(key.getBytes(), score, ByteArrayHelper.longToByteArray(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zaddStr(String key, double score, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zadd(key.getBytes(), score, member.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }


    @Override
    public Long zcard(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zcard(key.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zcount(String key, double min, double max) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zcount(key.getBytes(), min, max);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zrank(String key, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrank(key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zrem(String key, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrem(key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zrem2(String key, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrem(key.getBytes(), member.getBytes());
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    @Override
    public Long zremLong(String key, long member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrem(key.getBytes(), ByteArrayHelper.longToByteArray(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    @Override
    public Long zremrangeByRank(String key, long start, long end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zremrangeByRank(key.getBytes(), start, end);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zremrangebyscore(String key, String start, String end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zremrangeByScore(key, start, end);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<Object> zrevrange(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Object> returnSet = new ArrayList<Object>();
        try {
            Set<byte[]> set = jedis.zrevrange(key.getBytes(), start, end);
            for (byte[] s : set) {
                returnSet.add(ByteArrayHelper.byte2Object(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }

    @Override
    public List<Long> zrevrange3(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Long> returnSet = new ArrayList<>();
        try {
            Set<byte[]> set = jedis.zrevrange(key.getBytes(), start, end);
            for (byte[] s : set) {
                returnSet.add(Long.valueOf(ByteArrayHelper.byteArrayToLong(s)));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }

    @Override
    public List<Long> zrevrange2(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Long> returnSet = new ArrayList<Long>();
        try {
            Set<byte[]> set = jedis.zrevrange(key.getBytes(), start, end);
            for (byte[] s : set) {
                returnSet.add(ByteArrayHelper.byteArrayToLong(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }


    @Override
    public List<String> zrevrange4(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<String> returnSet = new ArrayList<>();
        try {
            Set<byte[]> set = jedis.zrevrange(key.getBytes(), start, end);
            for (byte[] s : set) {
                returnSet.add(new String(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }

    @Override
    public Set<Tuple> zrevrangeWithScores(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        Set<Tuple> set = new HashSet<>();
        try {
            set = jedis.zrevrangeWithScores(key.getBytes(), start, end);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return set;
    }

    @Override
    public Set<Tuple> zrangeWithScores(String key, int start, int end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        Set<Tuple> set = new HashSet<>();
        try {
            set = jedis.zrangeWithScores(key.getBytes(), start, end);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return set;
    }

    @Override
    public Long zrevrank(String key, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zrevrank(key.getBytes(), ByteArrayHelper.object2Bytes(member)) == null ? -1 : jedis.zrevrank(
                    key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zrevrank2(String key, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            Long position = jedis.zrevrank(key.getBytes(), member.getBytes());
            return position == null ? -1 : position;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long zrevrank3(String key, long member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            Long position = jedis.zrevrank(key.getBytes(), ByteArrayHelper.longToByteArray(member));
            return position == null ? -1 : position;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public long incr(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.incr(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public long incrBy(String key, long integer) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.incrBy(key, integer);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public long incrBy2(String key, long integer) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.incrBy(key, integer);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            throw e;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public double incrByFloat(String key, double member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.incrByFloat(key, member);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            throw e;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public long expire(String key, int seconds) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.expire(key, seconds);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    public long decr(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.decr(key);
        } catch (Exception e) {
            logger.error("redis incrBy err：", e);
            return 0;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    /**
     * REDIS自减指定值
     *
     * @param integer
     * @return
     */
    public long decrBy(String key, long integer) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.decrBy(key, integer);
        } catch (Exception e) {
            logger.error("redis incrBy err：", e);
            return 0;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Set<String> keys(String key) throws Exception {
        // TODO Auto-generated method stub
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.keys(key);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long rpush(String key, String... strings) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.rpush(key, strings);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public String lpopStr(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.lpop(key);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long hset(String key, String field, String value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hset(key, field, value);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Boolean hexists(String key, String field) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hexists(key, field);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return false;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public long hincrby(String key, String field, long integer) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hincrBy(key, field, integer);
        } catch (Exception e) {
            logger.error("redis hincrBy err：", e);
            return 0;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long hdel(String key, String... fields) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.hdel(key, fields);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long llen(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.llen(key);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<Object> zrange(String key, long start, long end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Object> ret = new ArrayList<>();
        try {
            Set<byte[]> set = jedis.zrange(key.getBytes(), start, end);
            for (byte[] i : set) {
                ret.add(ByteArrayHelper.byte2Object(i));
            }
            return ret;
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<String> zrange1(String key, long start, long end) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<String> ret = new ArrayList<>();
        try {
            Set<byte[]> set = jedis.zrange(key.getBytes(), start, end);
            for (byte[] i : set) {
                ret.add(new String(i));
            }
            return ret;
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return null;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }


    @Override
    public Long lrem(String key, long count, String value) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.lrem(key.getBytes(), count, ByteArrayHelper.object2Bytes(value));
//            return jedis.lrem(key,count,value);
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Double zscore(String key, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zscore(key.getBytes(), ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1d;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Double zscore2(String key, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zscore(key.getBytes(), member.getBytes());
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1d;
        } finally {
            getJedisPool().returnResource(jedis);
        }
    }

    @Override
    public Double zincrby(String key, double score, Object member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zincrby(key.getBytes(), score, ByteArrayHelper.object2Bytes(member));
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1d;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Double zincrbyStr(String key, double score, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zincrby(key.getBytes(), score, member.getBytes());
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1d;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Double zincrby2(String key, long score, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.zincrby(key.getBytes(), score, member.getBytes());
        } catch (Exception e) {
            logger.error("redis keys err：", e);
            return -1d;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Map<String, String> hgetAllStr(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.hgetAll(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long hsetStr(String key, String field, String value, int seconds) throws Exception {

        Jedis jedis = getJedisPool().getResource();
        try {
            long ret = jedis.hset(key, field, value);
            if (seconds > 0) {
                jedis.expire(key, seconds);
            }
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long hsetStr(String key, String field, String value) throws Exception {

        Jedis jedis = getJedisPool().getResource();
        try {
            long ret = jedis.hset(key, field, value);
            return ret;
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return -1l;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public String hgetStr(String key, String field) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.hget(key, field);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<String> hvals(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.hvals(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Set<String> smembers(String key) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            return jedis.smembers(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public List<Object> zrangeBySore(String key, double startScore, double endScore) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<Object> returnSet = Lists.newArrayList();
        try {
            Set<byte[]> set = jedis.zrangeByScore(key.getBytes(), startScore, endScore);
            for (byte[] s : set) {
                returnSet.add(ByteArrayHelper.byte2Object(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return returnSet;
    }

    public List<String> zrangeByScore1(String key, double startScore, double endScore) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<String> resList = Lists.newArrayList();
        try {
            Set<byte[]> set = jedis.zrangeByScore(key.getBytes(), startScore, endScore);
            for (byte[] s : set) {
                resList.add(new String(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return resList;
    }

    @Override
    public void ltrim(String key, int start, int end) throws Exception {
        Jedis jedis = null;
        try {
            jedis = getJedisPool().getResource();
            jedis.ltrim(key, start, end);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long scard(String key) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.scard(key);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    @Override
    public Long srem(String key, String member) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.srem(key, member);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }

    /**
     * Set（集合） 随机一个
     *
     * @param key
     * @param count
     * @return
     * @throws Exception
     */
    @Override
    public List<String> srandMember(String key, int count) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        try {
            return jedis.srandmember(key, count);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
    }


    @Override
    public List<String> zrangeByScoreLimit(String key, double startScore, double endScore, int offset, int count) throws Exception {
        Jedis jedis = getJedisPool().getResource();
        List<String> resList = Lists.newArrayList();
        try {
            Set<byte[]> set = jedis.zrangeByScore(key.getBytes(), startScore, endScore, offset, count);
            for (byte[] s : set) {
                resList.add(new String(s));
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        } finally {
            if (jedis != null) {
                getJedisPool().returnResource(jedis);
            }
        }
        return resList;
    }

}
